package org.bee.controller;

import org.bee.PageUtil;
import org.bee.ResultTool;
import org.bee.input.DictSelectVo;
import org.bee.json.JsonUtils;
import org.bee.model.DictEnums;
import org.bee.model.DictSelect;
import org.bee.model.result.Result;
import org.bee.redis.RedisUtils;
import org.bee.secuirty.annotation.ApiAuth;
import org.bee.string.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 枚举字典控制器
 */
@RestController
@RequestMapping("/enumDict")
public class EnumDictController {

    private final RedisUtils redisUtils;

    public EnumDictController(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }

    /**
     * 获取枚举列表
     * @param dictSelectVo 查询参数
     * @return 枚举列表
     */
    @PostMapping("enumDictPage")
    @ApiAuth(value = "enumDict.pageQuery",description = "获取枚举列表")
    public Result enumDictPage(@RequestBody DictSelectVo dictSelectVo){
        Result result = ResultTool.success();
        try {
            PageUtil.setPage(dictSelectVo.getCurrent(),dictSelectVo.getSize());
            long keyCount = 1L;
            Set<String> keys = new HashSet<>();
            if(StringUtils.isEmpty(dictSelectVo.getDictCode())){
                keys = redisUtils.keysPage("EnumDict:*",
                        dictSelectVo.getCurrent(), dictSelectVo.getSize());
                keyCount = redisUtils.getKeyCount("EnumDict:*");
            }else{
                String key = "EnumDict:"+dictSelectVo.getDictCode();
                keys.add(key);
            }
            List<DictEnums> enumDictList = new ArrayList<>();
            for (String key : keys) {
                String enumDictValue = (String) redisUtils.get(key);
                if(StringUtils.isNotEmpty(enumDictValue)){
                    DictEnums dictEnums = JsonUtils.fromJson(enumDictValue, DictEnums.class);
                    enumDictList.add(dictEnums);
                }else{
                    keyCount = 0;
                }
            }
            PageUtil.getPage().setTotal(keyCount);
            result.setPageData(enumDictList);
        } catch (Exception e) {
            result = ResultTool.fail();
            result.setMsg("查询异常");
            return result;
        } finally {
            PageUtil.clearPage();
        }
        return result;
    }

    /**
     * 获取枚举
     * @param dictCode 枚举标签
     * @return 枚举
     */
    @GetMapping("selectByCode")
    @ApiAuth(value = "enumDict.selectByCode",description = "获取枚举")
    public Result enumDict(String dictCode){
        Result result = ResultTool.success();
        try {
            String key = "EnumDict:"+dictCode;
            String enumDictValue = (String) redisUtils.get(key);
            if(StringUtils.isNotEmpty(enumDictValue)){
                DictEnums dictEnums = JsonUtils.fromJson(enumDictValue, DictEnums.class);
                result.setData(dictEnums);
            }else{
                result = ResultTool.fail();
                result.setMsg("查询异常");
            }
        } catch (Exception e) {
            result = ResultTool.fail();
            result.setMsg("查询异常");
        }
        return result;
    }
}
